/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author Alejandro
 */
@ManagedBean
@RequestScoped
public class UtilsBean {

    public String statusToString(Integer i) {
        String res = "Est. Desconocido";
        if (i.equals(0)) {
            res = "Eliminado";
        } else if (i.equals(1)) {
            res = "Activo";
        }
        return res;

    }

    public String itemStatusToString(Integer i) {
        String res = "Est. Desconocido";
        if (i.equals(0)) {
            res = "Eliminado";
        } else if (i.equals(1)) {
            res = "Activo";
        } else if (i.equals(2)) {
            res = "Sin stock";
        }
        return res;

    }

    public String permissionToString(Integer i) {
        String res = "Per. Desconocido";
        if (i.equals(2)) {
            res = "Administrador";
        } else if (i.equals(1)) {
            res = "Usuario";
        }
        return res;

    }

    public SelectItem[] statusList() {
        SelectItem[] items = {
            new SelectItem("", "Todos"),
            new SelectItem("Activo"),
            new SelectItem("Eliminado")
        };
        return items;
    }

    public SelectItem[] itemStatusList() {
        SelectItem[] items = {
            new SelectItem("", "Todos"),
            new SelectItem("Activo"),
            new SelectItem("Sin stock"),
            new SelectItem("Eliminado")
        };
        return items;
    }

    public String formatDate(Date date) {
        if (date == null) {
            return "Sin fecha";
        }
        SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm");
        return sdf.format(date);
    }
    
    public static String sha512(String texto)  {
        MessageDigest md;
        String output = "";
        try {
            md= MessageDigest.getInstance("SHA-512");
            md.update(texto.getBytes());
            byte[] mb = md.digest();
            for (int i = 0; i < mb.length; i++) {
                byte temp = mb[i];
                String s = Integer.toHexString(new Byte(temp));
                while (s.length() < 2) {
                    s = "0" + s;
                }
                s = s.substring(s.length() - 2);
                output += s;
            }
        } catch (Exception nsae) {
            nsae.printStackTrace();
        }
        
        return output;
    }
}
